package org.example.practiceexamples.mapper;

import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.dto.UserCourse;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class UserCourseResultSetExtractor implements ResultSetExtractor<UserCourse> {
    @Override
    public UserCourse extractData(ResultSet rs) throws SQLException{
        User user =null;
        List<Course> courses = new ArrayList<>();
        while(rs.next()){
            if(user==null){
                user = User.builder()
                        .id(rs.getString("u.id"))
                        .name(rs.getString("u.name"))
                        .phone(rs.getString("phone"))
                        .description(rs.getString("description"))
                        .role(rs.getString("role"))
                        .status(rs.getInt("status"))
                        .account(rs.getString("account"))
                        .password(rs.getString("password"))
                        .profession(rs.getString("profession"))
                        .updateTime(rs.getObject("update_time", LocalDateTime.class))
                        .createTime(rs.getObject("create_time", LocalDateTime.class))
                        .build();
            }
            Course c = Course.builder()
                    .id(rs.getString("c.id"))
                    .name(rs.getString("c.name"))
                    .lesson(rs.getInt("lesson"))
                    .credit(rs.getFloat("credit"))
                    .teacherId(rs.getString("teacher_id"))
                    .quantity(rs.getInt("quantity"))
                    .classNumber(rs.getString("class_number"))
                    .theoreticalHours(rs.getInt("theoretical_hours"))
                    .experimentalHours(rs.getInt("experimental_hours"))
                    .build();
            courses.add(c);
        }
        return new UserCourse(user, courses);
    }
}
